import _ from 'lodash'
// import baseURL from '_conf/url'
const baseURL = {
  url: '/api',
}

/**
 * 获取导出excel列
 * @param {Array} columns 表格列定义
 */
const fetchExportColumns = columns => {
  let array = []
  columns.forEach(v => {
    if (v.export !== false) {
      if (v.expands) {
        v.expands.forEach(expand => {
          array.push({ title: expand.title ? expand.title : v.title, field: expand.key })
        })
      } else {
        array.push({ title: v.title, field: v.key, format: v.format ? v.format : '' })
      }
    }
  })
  return JSON.stringify(array)
}

export default {
  data() {
    return {
      uploadModal: false,
    }
  },
  methods: {
    /**
     * 按钮业务权限
     * @param {Array} value 有效状态
     * @param {String} status 状态
     */
    statusList(value, status) {
      return _.includes(value, status)
    },
    /**
     * 返回上一页
     */
    onGoBack() {
      this.$router.go(-1)
    },
    /**
     * 操作确认提示
     */
    confirm({ title = '提示', content }) {
      return new Promise(resolve => {
        this.$Modal.confirm({
          title: title,
          content: content,
          onOk: () => {
            resolve()
          },
        })
      })
    },
    /**
     * 操作提示
     */
    message({ type, title = '提示', content, onOk = () => {} }) {
      switch (type) {
        case 'info':
          this.$Modal.info({
            title: title,
            content: content,
            onOk: onOk,
          })
          break
        case 'success':
          this.$Modal.success({
            title: title,
            content: content,
            onOk: onOk,
          })
          break
        case 'warning':
          this.$Modal.warning({
            title: title,
            content: content,
            onOk: onOk,
          })
          break
        case 'error':
          this.$Modal.error({
            title: title,
            content: content,
            onOk: onOk,
          })
          break
      }
    },
    /**
     * 导出表格Excel
     * @param {Function} apiFunction 导出Excel的API方法
     * @param {*} criteria
     * @param {*} columns
     */
    exportTable(apiFunction, criteria, columns) {
      apiFunction({ ...criteria, columnsJsonStr: fetchExportColumns(columns) }).then(res => {
        window.open(`${baseURL.url}${res.data}`)
      })
    },
    /**
     * 获取需要统计的列
     * @param {Array} columns 表格列定义
     */
    fetchAggregate(columns) {
      let aggregate = {}
      columns.forEach(v => {
        if (v.aggregate) {
          aggregate[v.key] = v.aggregate
        }
      })
      return JSON.stringify(aggregate)
    },
  },
}
